#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <cstring>

//user define
#include "axil_cmd.h"
#include "sensor_config.h"
#include "sensor_spi.h"
#include "sensor_user.h"

axil_cmd_handle sensor_spi;

int sensor_config_setup(unsigned int baseAddr){
    return axil_cmd_setup(&sensor_spi, baseAddr);
}

void sensor_spi_reset(){
    axil_cmd_set(&sensor_spi,SPI_RESET_OFFSET,0x1);
}

unsigned int sensor_spi_read(){
    return axil_cmd_get(&sensor_spi,SPI_STATUS_OFFSET);
}

//控制写 | 寄存器号 | 写入数据
void sensor_spi_write(unsigned int TX_ADDR,unsigned int TX_DATA){
    axil_cmd_set(&sensor_spi,SPI_CMD_OFFSET,(((0x1<<17) | ((TX_ADDR&0x1FF) << 8)) | (TX_DATA&0xFF)));
}

/*偏移到SPI_TX_OFFSET地址的32位中（默认全为0）：
 *7:0    是数据
 *16:8  是控制器地址，如要控制103号寄存器，需要写入这里
 *17     设置位为1：写，0：读
*/

//因为低8位为数据，16:8是需要操作的寄存器，所以左移8位
unsigned int sensor_spi_read(unsigned int RX_ADDR){
    axil_cmd_set(&sensor_spi,SPI_CMD_OFFSET,((RX_ADDR&0x1FF) << 8));
        return axil_cmd_get(&sensor_spi,SPI_RX_OFFSET);
}


